package cc.mrbird.web.controller;

import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtils;
import cc.mrbird.system.domain.*;
import cc.mrbird.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class ExcelExportController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private MidStudentService midStudentService;

    @Autowired
    private OneStudentService oneStudentService;

    @Autowired
    private RiseStudentService riseStudentService;

    @Autowired
    private VisitStudentService visitStudentService;

    @Value("${sys.admin}")
    private String admin;

    @RequestMapping("student/excel")
    @ResponseBody
    public ResponseBo studentExcel(Student student) {
        try {
            List<Student> students;
            if(student.getUserName().equalsIgnoreCase(admin)){
                student.setUserName("");
                students = this.studentService.findStudentByCondition(student);
            }else {
                students = this.studentService.findStudentByCondition(student);
            }
            return FileUtils.createExcelByPOIKit("专科考生信息表", students, Student.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @RequestMapping("mid_student/excel")
    @ResponseBody
    public ResponseBo midStudentExcel(MidStudent midStudent) {
        try {
            List<MidStudent> students;
            if(midStudent.getUserName().equalsIgnoreCase(admin)){
                midStudent.setUserName("");
                students = this.midStudentService.findMidStudentByCondition(midStudent);
            }else {
                students = this.midStudentService.findMidStudentByCondition(midStudent);
            }
            return FileUtils.createExcelByPOIKit("五年一贯制考生信息表", students, MidStudent.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @RequestMapping("rise_student/excel")
    @ResponseBody
    public ResponseBo riseStudentExcel(RiseStudent riseStudent) {
        try {
            List<RiseStudent> students;
            if(riseStudent.getUserName().equalsIgnoreCase(admin)){
                riseStudent.setUserName("");
                students = this.riseStudentService.findRiseStudentByCondition(riseStudent);
            }else {
                students = this.riseStudentService.findRiseStudentByCondition(riseStudent);
            }
            return FileUtils.createExcelByPOIKit("专升本考生信息表", students, RiseStudent.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @RequestMapping("one_student/excel")
    @ResponseBody
    public ResponseBo oneStudentExcel(OneStudent oneStudent) {
        try {
            List<OneStudent> students;
            if(oneStudent.getUserName().equalsIgnoreCase(admin)){
                oneStudent.setUserName("");
                students = this.oneStudentService.findOneStudentByCondition(oneStudent);
            }else {
                students = this.oneStudentService.findOneStudentByCondition(oneStudent);
            }
            return FileUtils.createExcelByPOIKit("一年制考生信息表", students, OneStudent.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    /**
     *
     * @param visitStudent
     * @return
     */
    @RequestMapping("visit_student/excel")
    @ResponseBody
    public ResponseBo visitStudentExcel(VisitStudent visitStudent) {
        try {
            List<VisitStudent> students;
            if(visitStudent.getUsername().equalsIgnoreCase(admin)){
                visitStudent.setUsername("");
                students = this.visitStudentService.findVisitStudentByCondition(visitStudent);
            }else {
                students = this.visitStudentService.findVisitStudentByCondition(visitStudent);
            }
            return FileUtils.createExcelByPOIKit("来校师生参观信息表", students, VisitStudent.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

}
